import i18n from '../../../i18n';

world.onPlayerJoin(async ({ entity }) => {
  entity.enableInteract = true;
  entity.interactRadius = 5;
  entity.interactHint = entity.player.name;
  const e = entity;
});
world.onInteract(async ({ entity, targetEntity }) => {
  if (!targetEntity.player) {
    return;
  }
  const d1 = await entity.player.dialog<GameSelectDialogParams>({
    type: GameDialogType.SELECT,
    title: i18n.t('interact.title', { lng: entity.language }),
    content: i18n.t('interact.content', {
      lng: entity.language,
      name: targetEntity.player?.name,
    }),
    options: [
      i18n.t('interact.garage', { lng: entity.language }),
      i18n.t('interact.give_car', { lng: entity.language }),
    ],
  });
  if (!d1) {
    return;
  }
  if (d1.index === 0) {
    const d2 = await entity.player.dialog<GameSelectDialogParams>({
      type: GameDialogType.SELECT,
      title: i18n.t('gallery.title', { lng: entity.language }),
      content: i18n.t('gallery.content', { lng: entity.language }),
      options: targetEntity.cars.map((car) => car.name),
    });
  } else if (d1.index === 1) {
    const d2 = await entity.player.dialog<GameSelectDialogParams>({
      type: GameDialogType.SELECT,
      title: i18n.t('interact.give_car', { lng: entity.language }),
      content: i18n.t('interact.give_car_content', { lng: entity.language }),
      options: entity.cars.map((car) => car.name),
    });
    if (!d2) {
      return;
    }
    const car = entity.cars.find((car) => car.name === d2.value);
    if (!car) {
      return;
    }
    entity.player.directMessage(
      i18n.t('interact.give_car_success', { lng: entity.language })
    );
    entity.cars.splice(entity.cars.indexOf(car), 1);
    targetEntity.cars.push(car);
    targetEntity.player?.directMessage(
      i18n.t('interact.other_give_car_success', { lng: targetEntity.language })
    );
  }
});
